package info.nixbear.tgcms.admin.client.activity;

import info.nixbear.tgcms.admin.client.place.RedirectsPlace;
import info.nixbear.tgcms.admin.client.place.RedirectsPlace.RedirectEditPlace;
import info.nixbear.tgcms.admin.client.view.RedirectEditView;
import info.nixbear.tgcms.admin.client.view.RedirectEditor;
import info.nixbear.tgcms.admin.shared.request.AdminRequestFactory;
import info.nixbear.tgcms.admin.shared.request.RedirectProxy;
import info.nixbear.tgcms.admin.shared.request.RedirectRequest;

import java.util.Set;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.activity.shared.Activity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.inject.Inject;
import com.google.web.bindery.requestfactory.gwt.client.RequestFactoryEditorDriver;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.Request;
import com.google.web.bindery.requestfactory.shared.RequestContext;
import com.google.web.bindery.requestfactory.shared.Violation;

public class RedirectEditActivity extends AbstractActivity {

	private RedirectEditView view;
	private PlaceController placeController;
	private AdminRequestFactory requestFactory;
	private RedirectEditPlace place;
	private Driver editorDriver;

	interface Driver extends RequestFactoryEditorDriver<RedirectProxy, RedirectEditor> { }

	@Inject
	public RedirectEditActivity(
			RedirectEditView view, 
			PlaceController placeController,
			AdminRequestFactory requestFactory) 
	{
		this.view = view;
		this.placeController = placeController;
		this.requestFactory = requestFactory;
	}

	public Activity withPlace(RedirectEditPlace place) {
		this.place = place;
		return this;
	}

	
	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		view.setRedirectEditActivity(this);
		panel.setWidget(view.asWidget());
		edit(); 
	}
	
	private void edit() {

		editorDriver = GWT.create(Driver.class);
		editorDriver.initialize(requestFactory, view.getEditor());

		RedirectProxy redirect = place.getRedirect();

		if(redirect == null) {
			
			if(place.isTokenANewToken()) {
				RedirectRequest request = requestFactory.redirectRequest();
				RedirectProxy tempalte2 = request.create(RedirectProxy.class);
				editorDriver.edit(tempalte2, request);
				request.put(tempalte2);
			} else {
			
				Request<RedirectProxy> fetchRequest = requestFactory.redirectRequest().get(Long.valueOf(place.getToken()));
				fetchRequest.with(editorDriver.getPaths());
				fetchRequest.to(
						new Receiver<RedirectProxy>() {
							@Override
							public void onSuccess(RedirectProxy page) {
	
								RedirectRequest request = requestFactory.redirectRequest();
	
								editorDriver.edit(page, request);
	
								request.put(page);
							}
						}).fire();
			}
		} else {

			Request<RedirectProxy> fetchRequest = requestFactory.find(redirect.stableId());
			fetchRequest.with(editorDriver.getPaths());
			fetchRequest.to(
					new Receiver<RedirectProxy>() {
						@Override
						public void onSuccess(RedirectProxy page) {

							RedirectRequest request = requestFactory.redirectRequest();

							editorDriver.edit(page, request);

							request.put(page);
						}
					}).fire();
		}
	}
	
	public void goTo(Place place) {
		placeController.goTo(place);
	}


	public void onSave(ClickEvent event) {
		// Flush the contents of the UI
		RequestContext context = editorDriver.flush();

		// Check for errors
		if (editorDriver.hasErrors()) {
			Window.alert("Errors detected locally");
			return;
		}

		// Send the request
		context.fire(new Receiver<Void>() {
			public void onViolation(Set<Violation> errors) {
				// Otherwise, show ConstraintViolations in the UI
				Window.alert("Errors detected on the server");
				editorDriver.setViolations(errors);
			}

			@Override
			public void onSuccess(Void response) {
				placeController.goTo(new RedirectsPlace());
			}
		});
	}
	
	public void onCancel(ClickEvent event) {
		placeController.goTo(new RedirectsPlace());
	}

	@Override
	public void onStop() {
		view.setRedirectEditActivity(null);
	}

	

}
